<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.CardTool");


class CardAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	/** 
	 * 贺卡列表
	 * 
	 */
	public  function card_list(){
			
	    $sql_cards_list 	=	get_sql_cards_list();
	    
	    $param = array(
				'result'	=>	$sql_cards_list,			//分页用的数组或sql
				'listvar'	=>	'card_list',				//分页循环变量
				'listRows'	=>	10,							//每页记录数
				//'parameter'=>'cat_id=' . $cat_id . "&intro_type=" . $intro_type . "&invalid=" . $invalid,//url分页后继续带的参数
				'target'	=>	'content',	    			//ajax更新内容的容器id，不带#
				'pagesId'	=>	'page',						//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
				'template'	=>	'Article:article_list',		//ajax更新模板
		);		
		// 分页
		$this->page_card_list($param);
		$this->display();
	}
	
	/**
	 * 贺卡分页
	 * Enter description here ...
	 */
	public function page_card_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}	
	
	/**
	 * 添加贺卡
	 * Enter description here ...
	 */
	public function add_card(){
	
	    /*初始化显示*/
	    $card['card_fee']   = 0;
	    $card['free_money'] = 0;
	
	    $this->assign('card',        $card);
	    $this->assign('form_action', 'insert');
	    $this->assign('action', 	 'add_card_ok');
		$this->display('card_info');
	}
	
	
	public function add_card_ok(){		
		import("@.Plugs.Exchange");
		import("ORG.Net.UploadFile");
				
		$exc 			= 	new exchange("ybr_card", null, 'card_id', 'card_name');
	    /*检查包装名是否重复*/
	    $is_only 		= 	$exc->is_only('card_name', $_POST['card_name']);	
	    if (!$is_only){
	        $this->error( sprintf( L('cardname_exist'), stripslashes($_POST['card_name'])) );
	    }
	
	    /*处理图片*/
	    $img_name			=	'';
		$upload 			= 	new UploadFile();
		$upload->maxSize  	= 	3145728 ;
		$upload->allowExts  = 	array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath 	=  	'./Public/Upload/images/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());		 
		}else{					// 上传成功 获取上传文件信息
			$info 			=  	$upload->getUploadFileInfo();
			$img_name		=	$info[0]['savename'];
		}
	    
	    /*插入数据*/
	    $sql 				= 	"INSERT INTO " . 'ybr_card' . "(card_name, card_fee, free_money, card_desc, card_img)
	            				VALUES ('$_POST[card_name]', '$_POST[card_fee]', '$_POST[free_money]', '$_POST[card_desc]', '$img_name')";
	    M()->query($sql);
	
	    admin_log($_POST['card_name'],'add','card');// 记录操作	
	    $this->success( $_POST['card_name'] . L('cardadd_succeed'), U('card/add_card') );
	}
	
	/**
	 * 修改贺卡
	 * Enter description here ...
	 */
	public function mod_card(){
	
		$CommonModel	=	new CommonModel();
	    $sql 			= 	"SELECT card_id, card_name, card_fee, free_money, card_desc, card_img FROM " . 'ybr_card' . " WHERE card_id='$_REQUEST[id]'";
	    $card 			= 	$CommonModel->GetRow($sql);
	
	    $this->assign('card',        	$card);
	    $this->assign('form_action', 	'update');
	    $this->assign('action', 		'mod_card_ok');		
		$this->display('card_info');
	}
	
	/**
	 * 处理修改贺卡
	 * Enter description here ...
	 */
	public function mod_card_ok(){	
		import("@.Plugs.Exchange");
		import("ORG.Net.UploadFile");
				
		$exc 				= 	new exchange("ybr_card", null, 'card_id', 'card_name');
		
	    if ($_POST['card_name'] != $_POST['old_cardname']){
	        /*检查品牌名是否相同*/
	        $is_only 		= 	$exc->is_only('card_name', $_POST['card_name'], $_POST['id']);	
	        if (!$is_only){	            
	            $this->error( sprintf( L('cardname_exist') , stripslashes($_POST['card_name'])) );
	        }
	    }
	    $param 				= 	"card_name = '$_POST[card_name]', card_fee = '$_POST[card_fee]', free_money= $_POST[free_money], card_desc = '$_POST[card_desc]'";
	    
	    /* 处理图片 */
	    $img_name 			= 	'';
		$upload 			= 	new UploadFile();
		$upload->maxSize  	= 	3145728 ;
		$upload->allowExts  = 	array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath 	=  	'./Public/Upload/images/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			Log::write("CardAction : mod_card_ok : 上传文件错误原因 --- " . $upload->getErrorMsg());
			
		}else{					// 上传成功 获取上传文件信息
			$info 			=  	$upload->getUploadFileInfo();
			$img_name		=	$info[0]['savename'];
		}
	    
	    if ($exc->edit($param,  $_POST['id'])){
	        admin_log($_POST['card_name'], 'edit', 'card');
	        	
	        $note 			= 	sprintf( L('cardedit_succeed') , $_POST['card_name']);
	        $this->success($note,U('card/card_list'));
	    }
	    else{
	       $this->error("操作失败");
	    }
	}

	/**
	 * 移除贺卡
	 * Enter description here ...
	 */
	public function remove_card(){		
		import("@.Plugs.Exchange");
				
		$exc 				= 	new exchange("ybr_card", null, 'card_id', 'card_name');		
	    $card_id 			= 	empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
	
	    $name 				= 	$exc->get_name($card_id);
	    $img 				= 	$exc->get_name($card_id, 'card_img');
	
	    if( is_array($exc->drop($card_id)) ){
	        /* 删除图片 */
	        if (!empty($img)){
	             @unlink('./Public/Upload/images/'.$img);
	        }
	        admin_log(addslashes($name),'remove','card');
	
	        $this->success("操作成功",U('card/card_list'));
	    }
	    else{
	        //make_json_error($db->error()); 
	        $this->error("操作失败",U('card/card_list'));
	    }
	}
		
}






